package web.rssReader;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import IO.Log;

import data.Episode;
import data.Show;

/**
 * RssReader
 * 
 * Template found @ http://javamix.wordpress.com/2009/06/04/read-rss-feeds-by-using-java/
 * @author katta vijay
 * @modifier Bionic Weasel
 */
public class EztvRssReader extends RssReaderInterface
{

      public EztvRssReader(String showName)
      {
              this.stringURL = "http://www.ezrss.it/search/index.php?show_name="+
              showName.replace(' ', '+').toLowerCase()+
              "&date=&quality=&release_group=&mode=rss";
              
              this.showName = showName;
              this.connect();
      }
      /**
       * Gets the shows from the RSS Feed and returns them in a LinkedList<Show>
       * @return Shows in a LinkedList.
       */
      public Show getShow()
      {
              if(this.url == null)
              {
            	  Log.printErrorln("this.url is not declared!");
              }
              
              Show show = new Show(showName);
              
              try 
              {       
                      DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
                      factory.setValidating(false);
                      factory.setFeature("http://xml.org/sax/features/namespaces", false);
                      factory.setFeature("http://xml.org/sax/features/validation", false);
                      factory.setFeature("http://apache.org/xml/features/nonvalidating/load-dtd-grammar", false);
                      factory.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);

                      DocumentBuilder builder = factory.newDocumentBuilder();
                      Document doc = builder.parse(this.url.openStream());
                      
                      NodeList nodes = doc.getElementsByTagName("item");
      
                      for(int i=0;i<nodes.getLength();i++)    
                      {
                              Element element = (Element)nodes.item(i);
                              String epTitle = null;
                              int epNum = 0;
                              int seasonNum = 0;
                              String link;
                              String pubDate;
                              
                              String[] tempString = new String[VALUE];
                              String[][] description = new String[VALUE][2];
                              
                              
                              tempString= getElementValue(element,"description").split("; ");
                              
                              for(int j=0; j<tempString.length; j++)
                              {
                                      description[j] = tempString[j].split(": ");
                                      if(description[j][0].equals("Episode Title"))
                                      {
                                              epTitle = description[j][1];
                                      }
                                      /** added by by Bionic Weasel for daily support*/
                                      else if(description[j][0].equals("Episode Date"))
                                      {
                                    	    if(description[j].length >= 2)
                                    	    {
                                    	    	String strDate = description[j][1].replace("-", "");
                                                epNum = Integer.parseInt(strDate);
                                    	    }
                                      }
                                      else if(description[j][0].equals("Episode"))
                                      {
                                              epNum = Integer.parseInt(description[j][1]);
                                      }
                                      else if(description[j][0].equals("Season"))
                                      {
                                              seasonNum = Integer.parseInt(description[j][1]);
                                      }
                                      else if(description[j][0].equals("Show Name"))
                                      {
                                              showName = description[j][1];
                                      }
                              }
                              link = getElementValue(element,"link");
                              
                              //pubdate has a format like:  "Sat, 21 May 2011 00:40:04 -0500"
                              pubDate = getElementValue(element,"pubDate");
                              
                              SimpleDateFormat dateFormat = new SimpleDateFormat( "dd MMM yyyy hh:mm:ss Z" );
                              pubDate = pubDate.substring(5);
                              Date date = new Date();
                              try {
                                      date = dateFormat.parse( pubDate );
                              } catch (ParseException e) {
                            	  Log.printErrorln("Date-parse fail for EztvRssReader");
                              }
                              
                              Episode ep = new Episode(epNum, epTitle, link, "", date);
                      
                              show.addEpisode(ep, seasonNum);
                      }
                      
              }
              catch(Exception ex) 
              {
            	  	Log.printErrorln(ex.getLocalizedMessage());
                      ex.printStackTrace();
              }
              return show;
      }
}